package com.tsingteng.cosfun.ui.message.comment.videocomment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.AddVideoCommentResultBean;
import com.tsingteng.cosfun.bean.AttentFansbean;
import com.tsingteng.cosfun.bean.MessageVideoCommentBean;
import com.tsingteng.cosfun.callback.OnPlayDetailClickListener;
import com.tsingteng.cosfun.common.Navigate;
import com.tsingteng.cosfun.ui.cosfun.AtFragment;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.ui.mine.TerminalActivity;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.widget.dialog.BaseBottomSheetFragment;
import com.tsingteng.cosfun.widget.dialog.BottomListDialog;
import com.tsingteng.cosfun.widget.dialog.CommentPlayDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * @我。赞我。评论我页面点击展示视频评论页 常胜
 * 2018.8.23
 */
public class VideoCommentFragment extends BaseBottomSheetFragment implements VideoCommentContract.IVideoCommentView, BaseQuickAdapter.OnItemClickListener, OnPlayDetailClickListener {
    private int id;
    private int page = 1;
    private String reply_id = "";
    private boolean isReply = false;
    private TextView user_comm_count;
    private RelativeLayout top_rel;
    private RecyclerView user_comm_recycler;
    private EditText user_comm_et;
    private TextView user_comm_at;
    private FrameLayout frameLayout;
    private RelativeLayout rela;
    private TextView user_comm_empty;
    private VideoCommentAdapter videoCommentAdapter;
    private VideoCommentPresenter mPresenter;
    private ImageView mCreat1;
    private MessageVideoCommentBean.DataBean.CommentListBean item1;
    private ImageView mIvClose;
    private int commonId;

    public static VideoCommentFragment newInstance(int id,String commonId) {
        VideoCommentFragment videoCommentFragment = new VideoCommentFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        bundle.putString("commonId",commonId);
        videoCommentFragment.setArguments(bundle);
        return videoCommentFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_comment, container, false);
        initView(view);
        Bundle bundle = getArguments();
        id = bundle.getInt("workId");
        commonId = bundle.getInt("commonId");
        videoCommentAdapter = new VideoCommentAdapter();
        user_comm_recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        user_comm_recycler.setAdapter(videoCommentAdapter);
        videoCommentAdapter.setOnItemClickListener(this);
        videoCommentAdapter.setOnPlayDetailClickListener(this);
        mPresenter = new VideoCommentPresenter(this);
        mPresenter.getVideoCommentList(page, id,commonId==0?"":String.valueOf(commonId));


        mIvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        user_comm_et.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showCommentDialog("",null);
            }
        });
        user_comm_at.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AtFragment atFragment = AtFragment.newInstance();
                atFragment.setOnItemSelectLisenter(new AtFragment.OnItemSelectLisenter() {
                    @Override
                    public void selected(AttentFansbean.Data data) {
                        showCommentDialog("",data);
                        atFragment.dismiss();
                    }
                });
                if (null ==getActivity()){
                    return;
                }
                atFragment.show(getActivity().getSupportFragmentManager(), "myFragment");
            }
        });
        return view;
    }


    private void showCommentDialog(String commonid, AttentFansbean.Data data) {
        CommentPlayDialog dialog = new CommentPlayDialog(commonid, data, new CommentPlayDialog.CommentPlayDialogListener() {
            @Override
            public void commSuccess(String commStr) {
                if (isReply) {
                    mPresenter.getMyVideoComment(id + "", commonid, String.valueOf(LogonUtils.getProFildId()), commStr, 1);
                } else {
                    mPresenter.getMyVideoComment(id + "", "", String.valueOf(LogonUtils.getProFildId()), commStr, 1);
                }
            }
        });
        dialog.show(getActivity().getSupportFragmentManager(), "comment");
    }
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
      MessageVideoCommentBean.DataBean.CommentListBean commentListBean = (MessageVideoCommentBean.DataBean.CommentListBean) videoCommentAdapter.getData().get(position);
      /*    commentId = commentListBean.getCommentId();
        pointerUserId = commentListBean.getPointerUserId();
        reply_id = commentId + "";
        user_comm_et.setText("");

        isReply = true;
        showCommentDialog("",null);*/

        final int index = position;
        if (commentListBean.getCommentUserId() == LogonUtils.getProFildId()) {
            List<String> list = new ArrayList<>();
            list.add("删除");
            BottomListDialog bottomListDialog = new BottomListDialog(getActivity());
            bottomListDialog.setList(list);
            bottomListDialog.setOnClickBottomListListener(new BottomListDialog.OnClickBottomListListener() {
                @Override
                public void onClickItem(BaseQuickAdapter adapter, View view, int position, List<String> list) {
                    videoCommentAdapter.remove(index);
                    adapter.notifyDataSetChanged();
                    mPresenter.commDel(String.valueOf(commentListBean.getCommentId()));


                }

                @Override
                public void onClickBottom() {

                }
            });
            bottomListDialog.show();
        } else {
//            dialog = builder.setMessage(position, workId, commId, "").setItemClickListener(this).createDialog();
//            dialog.show();

            List<String> list = new ArrayList<>();
            list.add("回复");
            list.add("举报");
            BottomListDialog bottomListDialog = new BottomListDialog(getActivity());
            bottomListDialog.setList(list);
            bottomListDialog.setOnClickBottomListListener(new BottomListDialog.OnClickBottomListListener() {
                @Override
                public void onClickItem(BaseQuickAdapter adapter, View view, int position, List<String> list) {
                    if (position == 0) {
                        isReply = true;
                        showCommentDialog(String.valueOf(commentListBean.getCommentId()),null);
                    } else if (position == 1) {
                        int type = 3;
                        Navigate.startReportList(getContext(), type, commentListBean.getCommentId());
                    }
//                    deleteComm(index);
                }

                @Override
                public void onClickBottom() {

                }
            });
            bottomListDialog.show();
        }
    }

    @Override
    public void showVideoCommentListResult(MessageVideoCommentBean messageVideoCommentBean) {
        if (messageVideoCommentBean==null || messageVideoCommentBean.getData()==null || messageVideoCommentBean.getData().getCommentList()==null)return;
        videoCommentAdapter.addData(messageVideoCommentBean.getData().getCommentList());
        videoCommentAdapter.notifyDataSetChanged();
        user_comm_count.setText("评论 "+messageVideoCommentBean.getData().getCount());
    }

    @Override
    public void showMyVideoCommentResult(AddVideoCommentResultBean addVideoCommentResultBean) {
        Toast.makeText(getContext(), "评论成功！", Toast.LENGTH_SHORT).show();
        mPresenter.getVideoCommentList(page, id,commonId==0?"":String.valueOf(commonId));
        isReply= false;
    }

    @Override
    public void showPriseCount(int status) {
            if (item1.getIsLike()==0) {
                item1.setIsLike(1);
                int num = Integer.valueOf(item1.getLikeTimes()) + 1;
                item1.setLikeTimes(num);
                Toast.makeText(getContext(), "点赞成功!", Toast.LENGTH_SHORT).show();
                videoCommentAdapter.notifyDataSetChanged();
            }else {
                int num = Integer.valueOf(item1.getLikeTimes()) -1;
                item1.setLikeTimes(num);
                Toast.makeText(getContext(), "取消点赞成功!!", Toast.LENGTH_SHORT).show();
                item1.setIsLike(0);
                videoCommentAdapter.notifyDataSetChanged();
            }
    }

    @Override
    public void showLoading(String msg) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showFail(String msg) {

    }

    @Override
    public void showError() {

    }

    @Override
    public void showEmpty() {

    }


    private void initView(View view) {
        user_comm_count = (TextView) view.findViewById(R.id.user_comm_count);
        top_rel = (RelativeLayout) view.findViewById(R.id.top_rel);
        user_comm_recycler = (RecyclerView) view.findViewById(R.id.user_comm_recycler);
        user_comm_et = (EditText) view.findViewById(R.id.user_comm_et);
        user_comm_at = (TextView) view.findViewById(R.id.user_comm_at);
        frameLayout = (FrameLayout) view.findViewById(R.id.frameLayout);
        rela = (RelativeLayout) view.findViewById(R.id.rela);
        user_comm_empty = (TextView) view.findViewById(R.id.user_comm_empty);
        mIvClose = (ImageView) view.findViewById(R.id.iv_close);
    }

    @Override
    public void onPraiseClick(MessageVideoCommentBean.DataBean.CommentListBean item, ImageView mCreat) {
        mCreat1 = mCreat;
        item1 = item;
        if (item1.getIsLike()==1) {
            mPresenter.getPriseCount(String.valueOf(item.getCommentId()),0);
        }else {
            mPresenter.getPriseCount(String.valueOf(item.getCommentId()),1);
        }
    }

    @Override
    public void onUserClick(String userId) {
        Bundle bundle = new Bundle();
        bundle.putInt("user_id", Integer.parseInt(userId));
        bundle.putInt("tag", 1);
        TerminalActivity.showFragment(getActivity(), HomeMineFragment.class, bundle);
    }
}
